package cn.xshi.monitor.web;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import cn.xshi.monitor.service.IotDeviceTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.xshi.common.annotation.Auth;
import cn.xshi.common.base.BaseAction;
import cn.xshi.common.base.BasePage;
import cn.xshi.common.base.BaseResult;
import cn.xshi.common.base.BaseSearch;
import cn.xshi.common.entity.OauthAccountEntity;
import cn.xshi.common.util.StringUtil;
import cn.xshi.monitor.model.IotDeviceType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageInfo;

import javax.annotation.Resource;
/**
 * @Desc 设备类型
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/iotDeviceType")
@Api(value = "设备类型",tags = "设备类型", description = "设备类型")
public class IotDeviceTypeController extends BaseAction {

	@Resource
	private IotDeviceTypeService iotDeviceTypeService;

	/**
	 * 查询并分页
	 * @param baseSearch
	 */
	@ApiOperation(value="查询并分页", notes="查询并分页")
	@PostMapping(value="/list")
	@Auth(value = "/iotDeviceType/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BasePage<List<IotDeviceType>> getIotDeviceTypeListByCondition(@RequestBody(required=true)BaseSearch baseSearch){
		Map<String, Object> condition = baseSearch.convert();
		commonHPager(baseSearch);
		List<IotDeviceType> iotDeviceTypeList = iotDeviceTypeService.getIotDeviceTypeListByCondition(condition);
		for(IotDeviceType iotDeviceType:iotDeviceTypeList){
			if(!StringUtil.isEmpty(iotDeviceType.getCreateId())){
				OauthAccountEntity createBy = getAccount(iotDeviceType.getCreateId());
				if(null != createBy){
					iotDeviceType.setCreateBy(createBy.getName());
				}
			}
			if(!StringUtil.isEmpty(iotDeviceType.getUpdateId())){
				OauthAccountEntity modifiedBy = getAccount(iotDeviceType.getUpdateId());
				if(null != modifiedBy){
					iotDeviceType.setModifiedBy(modifiedBy.getName());
				}
			}
		}
		PageInfo<IotDeviceType> page = new PageInfo<IotDeviceType>(iotDeviceTypeList);
		return outPageBootStr(page,baseSearch);
	}

	/**
	* 查询单条记录
	* @param id
	*/
	@ApiOperation(value="查询单条记录", notes="查询单条记录")
	@GetMapping(value="/get/{id}")
	@Auth(value = "/iotDeviceType/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<IotDeviceType> getIotDeviceTypeById(@PathVariable("id")String id){
		IotDeviceType iotDeviceType = iotDeviceTypeService.getIotDeviceTypeById(id);
		if(!StringUtil.isEmpty(iotDeviceType.getCreateId())){
			OauthAccountEntity createBy = getAccount(iotDeviceType.getCreateId());
			if(null != createBy){
				iotDeviceType.setCreateBy(createBy.getName());
			}
		}
		if(!StringUtil.isEmpty(iotDeviceType.getUpdateId())){
			OauthAccountEntity modifiedBy = getAccount(iotDeviceType.getUpdateId());
			if(null != modifiedBy){
				iotDeviceType.setModifiedBy(modifiedBy.getName());
			}
		}
		return outDataStr(iotDeviceType);
	}

	/**
	* 添加
	* @param iotDeviceType 
	*/
	@ApiOperation(value="添加", notes="添加")
	@PostMapping(value="/add")
	public BaseResult addIotDeviceType(@RequestBody IotDeviceType iotDeviceType){
		int i = 0;
		if(null != iotDeviceType){
			i=iotDeviceTypeService.addIotDeviceType(iotDeviceType);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	* 修改
	* @param iotDeviceType 
	*/
	@ApiOperation(value="修改", notes="修改")
	@PutMapping(value="/update")
	public BaseResult updateIotDeviceType(@RequestBody IotDeviceType iotDeviceType){
		int i = 0;
		if(null != iotDeviceType){
			i=iotDeviceTypeService.updateIotDeviceType(iotDeviceType);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	* 删除
	* @param id
	*/
	@ApiOperation(value="删除", notes="删除")
	@DeleteMapping(value="/delete")
	public BaseResult delIotDeviceType(String id){
		int i = 0;
		if(!StringUtil.isEmpty(id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("id",id.split(","));
			i=iotDeviceTypeService.delIotDeviceType(condition);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 查找全部类型
	 */
	@ApiOperation(value="查找全部类型", notes="查找全部类型")
	@GetMapping(value="/allList")
	@Auth(value = "/iotDeviceType/allList",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<List<IotDeviceType>> allList(){
		Map<String,Object> condition = new HashMap<>();
		List<IotDeviceType> iotDeviceTypeList = iotDeviceTypeService.getIotDeviceTypeListByCondition(condition);
		return new BaseResult(iotDeviceTypeList);
	}

	/**
	 * 查找相机全部类型
	 */
	@ApiOperation(value="查找相机全部类型", notes="查找相机全部类型")
	@GetMapping(value="/videoTypeList")
	@Auth(value = "/iotDeviceType/videoTypeList",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<List<IotDeviceType>> getVideoTypeList(){
		Map<String,Object> condition = new HashMap<>();
		List<IotDeviceType> iotDeviceTypeList = iotDeviceTypeService.getVideoTypeList(condition);
		return new BaseResult(iotDeviceTypeList);
	}
}
